from sqlalchemy import Column, String, Integer, TIMESTAMP, ForeignKey
from app.db.base import Base
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship

class Session(Base):
    __tablename__ = "sessions"

    id = Column(String(50), primary_key=True, index=True)  # 已改为字符串
    user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"))
    title = Column(String(100))
    created_at = Column(TIMESTAMP, server_default=func.now())
    updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())

    user = relationship("User", backref="sessions")

    # 这里用字符串形式引用类，避免循环引用问题
    messages = relationship("Message", back_populates="session")
    history = relationship("History", back_populates="session")
